package javabase.thinkinjava.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import static javabase.thinkinjava.nio.GetChannel.BSIZE;

/**
 * @author fance
 * @date 18-4-30 上午8:12
 */
public class ChannelCopy {
    public static void main(String[] args) throws IOException {
        FileChannel fileChannelIn = new FileInputStream("data.txt").getChannel();
        FileChannel fileChannelOut = new FileOutputStream("data2.txt").getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
        while (fileChannelIn.read(buffer) != -1) {
            buffer.flip();
            fileChannelOut.write(buffer);
            buffer.clear();
        }

        // transferTo transferForm
        fileChannelIn.transferTo(0, fileChannelIn.size(),fileChannelOut);
    }
}
